package com.ddwl.goods.dao.bargain;

import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.goods.mapper.bargain.BargainLogMapper;
import com.ddwl.goods.model.bargain.BargainLog;
import com.ddwl.schema.bo.goods.bargain.BargainLogPageBo;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.List;

/**
 * @Author hemiaojun
 * @Date 2020/10/26 11:47
 */
@Repository
public class BargainLogDao extends TkDao<BargainLogMapper, BargainLog> {

    @Resource
    private BargainLogMapper bargainLogMapper;

    public List<BargainLog> getList(BargainLogPageBo param) {
        return getListByBargainOrderId(param.getBargainOrderId());
    }

    public List<BargainLog> getListByBargainOrderId(String bargainOrderId){
        BargainLog bl = new BargainLog();
        bl.setBargainOrderId(bargainOrderId);
        return select(bl);
    }

    public Integer countByBargainOrderId(String bargainOrderId) {
        BargainLog bl = new BargainLog();
        bl.setBargainOrderId(bargainOrderId);
        bl.setDflag(BaseEnum.DflagEnum.NOT_DELETE.getCode());
        return bargainLogMapper.selectCount(bl);
    }

    public BargainLog getByBargainOrderIdAndUserId(String bargainOrderId, String userId) {
        BargainLog bl = new BargainLog();
        bl.setBargainOrderId(bargainOrderId);
        bl.setUserId(userId);
        return selectOne(bl);
    }

    public List<BargainLog> getByUserId(String userId) {
        BargainLog bl = new BargainLog();
        bl.setUserId(userId);
        return select(bl);
    }

    public Integer countWithinPeriod(String userId,Instant startTime, Instant endTime) {
        Example example = getExample();
        example.and().andEqualTo("userId", userId)
                .andGreaterThanOrEqualTo("gmtCreated", startTime)
                .andLessThanOrEqualTo("gmtCreated", endTime);
        return bargainLogMapper.selectCountByExample(example);
    }
}
